package com.bw.z.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.ColorRes;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.bw.z.R;

public class TimeListView extends View {
    private Bitmap selBit;//已经完成签到任务图片
    private Bitmap popBit;//翻倍气泡背景图
    private int selColor;//已完成任务
    private int normalColor;//默认任务颜色
    private String[] names = {"1连签", "2连签", "3连签", "4连签", "5连签", "6连签", "7连签"};
    private String[] numes = {"5", "10", "10", "20", "25", "25", "50"};
    private int width = 0;//空间的宽度
    private int height = 0;//控件的高度
    private int selIndex = 0;//当前签到进度
    private int itemWidth = 0;//每哥圈或线的宽度
    private int popHeight = 0;//弹窗高度
    private int tvHeight = ConvertUtils.dp2px(20);//嵌套文字高度

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);


    public TimeListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        itemWidth = width / 13;
    }

    private void init() {
        selBit = BitmapFactory.decodeResource(getResources(), R.mipmap.res_7icon_362);
        popBit = BitmapFactory.decodeResource(getResources(), R.mipmap.module_integral_double_bg);
        selColor = Color.RED;
        normalColor = Color.GRAY;
        popHeight = popBit.getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSign(canvas);
    }

    private void drawSign(Canvas canvas) {

        for (int i = 0; i < 13; i++) {
            if (i % 2 == 0) {
                paint.setColor(normalColor);
                if (i <= selIndex * 2) {
                    paint.setColor(selColor);
//                    LogUtils.e("ZLQ:RED");
                }

                if (i == 6 || i == 12) {
                    paint.setColor(Color.RED);
                }
                canvas.drawCircle(itemWidth * i + itemWidth / 2,
                         popHeight + (height - popHeight - tvHeight) / 2,
                        itemWidth / 2, paint);
                paint.setColor(Color.WHITE);
                canvas.drawCircle(itemWidth * i + itemWidth / 2,
                        popHeight + (height - popHeight - tvHeight) / 2,
                        itemWidth / 2 - ConvertUtils.dp2px(1), paint);
                /*绘制圆圈中间的红色文字签到积分*/
                paint.setColor(normalColor);
                if (i == 6 || i == 12) {
                    paint.setColor(selColor);
//                    LogUtils.e("ZLQ:RED");
                }
                paint.setTextSize(itemWidth / 2);
                paint.setFakeBoldText(true);
                paint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(numes[i / 2],
                        itemWidth * i + itemWidth / 2,
                        popHeight + (height - popHeight - tvHeight) / 2 + (itemWidth / 4),
                        paint);
                paint.setColor(normalColor);
                /*绘制气泡弹窗*/
                if (i == 6 || i == 12) {
                    Rect rect = new Rect();
                    rect.left = itemWidth * i + ConvertUtils.dp2px(1);
                    rect.right = itemWidth * i + popBit.getWidth() - ConvertUtils.dp2px(16);
                    rect.top = 0;
                    rect.bottom = popHeight - popHeight / 2;
                    canvas.drawBitmap(popBit, null, rect, paint);
                    paint.setTextSize(itemWidth / 3);
                    paint.setColor(Color.WHITE);
                    canvas.drawText("翻倍", itemWidth * i + itemWidth - ConvertUtils.dp2px(14),
                            popHeight / 2 - ConvertUtils.dp2px(3), paint);
                    /*设置连签文字颜色*/
                    paint.setColor(selColor);
                }
                paint.setTextSize(itemWidth / 3);
                canvas.drawText(names[i / 2],
                        itemWidth * i + itemWidth / 2,
                        height - 2/*ConvertUtils.dp2px(5)*/,
                        paint);
                /*绘制用户已签到图片*/
                if (i <= selIndex * 2) {
                    //使用rect限制图片绘制区域,保证图片绘制在view上时对应的圈以内
                    Rect dct = new Rect();
                    dct.left = itemWidth * i + ConvertUtils.dp2px(1);
                    dct.right = itemWidth * (i + 1) - ConvertUtils.dp2px(1);
                    dct.top = popHeight + (height - popHeight - tvHeight) / 2 - (itemWidth - 1) / 2;
                    dct.bottom = popHeight + (height - popHeight - tvHeight) / 2 + (itemWidth - 1) / 2;
                    canvas.drawBitmap(selBit, null, dct, null);
                }
            } else {
                paint.setColor(normalColor);
                canvas.drawLine(itemWidth * i,
                        popHeight + (height - popHeight - tvHeight) / 2 - ConvertUtils.dp2px(1) / 3, itemWidth * (i + 1),
                        popHeight + (height - popHeight - tvHeight) / 2, paint);
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (selBit != null) {
            selBit.recycle();
        }
        if (popBit != null) {
            popBit.recycle();
        }
    }
}
